package net.sk.china.platform.web;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sk.china.common.model.system.SystemConfig;
import net.sk.china.platform.request.system.SystemConfigAdminRequest;
import net.sk.china.platform.request.system.SystemFormCheckRequest;
import net.sk.china.platform.service.system.SystemConfigService;
import net.sk.china.common.response.CommonResult;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * +----------------------------------------------------------------------
 * // | Talk To [ 聊来改善地区金融服务环境，助力企业发展 ]
 * // +----------------------------------------------------------------------
 * // | Copyright (c) 2018-2024 聊来 All rights reserved.
 * // +----------------------------------------------------------------------
 * // | Licensed ( <a href="http://www.apache.org/licenses/LICENSE-2.0">apache</a> )
 * // +----------------------------------------------------------------------
 * // | @Author: 聊来 <18970881148@qq.com>
 * // +----------------------------------------------------------------------
 * // | DateTime: 2024/6/2 16:09
 * // +----------------------------------------------------------------------
 */

@RestController
@RequestMapping("system/config/")
@Api(tags = "系统设置")
public class SystemConfigController {

    private final SystemConfigService systemConfigService;

    public SystemConfigController(SystemConfigService systemConfigService) {
        this.systemConfigService = systemConfigService;
    }


    @PreAuthorize("hasAuthority('admin:system:config:info')")
    @ApiOperation(value = "详情")
    @RequestMapping(value = "info", method = RequestMethod.GET)
    public CommonResult<HashMap<String, String>> info(@RequestParam(value = "formId") Integer formId) {
        return CommonResult.success(systemConfigService.info(formId));
    }


    @PreAuthorize("hasAuthority('admin:system:config:save:form')")
    @ApiOperation(value = "整体保存表单数据")
    @RequestMapping(value = "save/form", method = RequestMethod.POST)
    public CommonResult<String> saveFrom(@RequestBody @Validated SystemFormCheckRequest systemFormCheckRequest) {
        if (systemConfigService.saveForm(systemFormCheckRequest)) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @PreAuthorize("hasAuthority('admin:system:config:check')")
    @ApiOperation(value = "检测表单name是否存在")
    @RequestMapping(value = "check", method = RequestMethod.GET)
    public CommonResult<Boolean> check(@RequestParam String name) {
        return CommonResult.success(systemConfigService.checkName(name));
    }

    @PreAuthorize("hasAuthority('admin:system:config:saveunique')")
    @ApiOperation(value = "表单配置中仅仅存储")
    @RequestMapping(value = "save/unique", method = RequestMethod.POST)
    public CommonResult<Boolean> justSaveUniq(@RequestParam String key, @RequestParam String value) {
        return CommonResult.success(systemConfigService.updateOrSaveValueByName(key, value));
    }


    @PreAuthorize("hasAuthority('admin:system:config:getunique')")
    @ApiOperation(value = "表单配置根据key获取")
    @RequestMapping(value = "unique", method = RequestMethod.GET)
    public CommonResult<Object> justGetUniq(@RequestParam String key) {
        return CommonResult.success(systemConfigService.getValueByKey(key),"success");
    }


    @PreAuthorize("hasAuthority('admin:system:config:get')")
    @ApiOperation(value = "根据key获取配置")
    @RequestMapping(value = "get", method = RequestMethod.GET)
    public CommonResult<List<SystemConfig>> getByKey(@RequestParam String key) {
        return CommonResult.success(systemConfigService.getListByKey(key));
    }

    @PreAuthorize("hasAuthority('admin:system:config:update')")
    @ApiOperation(value = "更新配置信息")
    @RequestMapping(value = "update", method = RequestMethod.POST)
    public CommonResult<List<SystemConfig>> getByKey(@RequestBody @Validated List<SystemConfigAdminRequest> requestList) {
        if (systemConfigService.updateByList(requestList)) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }
}
